package com.fl.proyect_softmedic;

import java.util.Calendar;

import com.example.proyect_softmedic.R;

import android.os.Bundle;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;

public class OpenschedulleActivity extends Activity {

	//campos de texto donde se almacenaran las elecciones hechas por el usuario
	private EditText editxt1, editxt2, editxthourFromAm, editxthourToAm, editxthourFromPm, editxthourToPm, editxtidschedulle, editxtx_id_med; 
	private Calendar cal; // variabe para obtener datos de fecha y hora del sistema
	private int day; // variable para almacenar el dia actual
	private int month;// variable para almacenar el mes actual
	private int year;// variable para almacenar el anyo actual
	private Button btnDateFrom, btndateto; // botones para las selecciones de fechas de inicio y fecha final
	private Button btn_hourFromAm, btn_hourToAm, btn_hourFromPm, btn_hourToPm; // botonos para la seleccion de horas
	private int pHour; // variable apara almacenar la hora seleccionada por el usuario
	private int pMinute;// variable apara almacenar los minutos seleccionados por el usuario
	
	//variable para almacenar el roll del usuario
    private int roll_user;
  //boton para regresar al menu de inicio
    private Button btnstart;
    
    private CheckBox monday, tuesday, wednesday, thursday, friday, saturday;
   
    private CustomArrayAdapter<CharSequence> mAdapter;
    private Spinner sp_duration; 
    
    
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_openschedulle);
		
		
		
		btnstart = (Button)findViewById(R.id.btnGoStart);
		btnstart.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				roll_user = 1;//(int) (Math.random()*2+1);
				if(roll_user==1){
					Intent i = new Intent(OpenschedulleActivity.this, Activity_medic.class);
					startActivity(i);
				}else{
					//Intent i = new Intent(ViewchangeschedulleActivity.this, Activity_medic.class);
					//startActivity(i);
				}
				
			}
		});
		
		//definimos los buttons para seleccionar el rango de fechas
		btnDateFrom = (Button)findViewById(R.id.btnDateFrom);
		btndateto = (Button)findViewById(R.id.btnDateTo);
		
		//definimos lo campos de etexto donde se almacena las fechas seleccionadas
		editxt2 = (EditText)findViewById(R.id.edtxtDateTo);
		editxt2.setKeyListener(null);
		editxt1 = (EditText)findViewById(R.id.edtxt_get_name_user);
		editxt1.setKeyListener(null);
		//definimos los cajas de texto para mostrar el id de la agenda, id del medico y tipo de agendamiento
		editxtidschedulle = (EditText)findViewById(R.id.edtxtIdSchedulle);
		//a cada campo asignamos a null los eventos de key, para hacer los campos no editables
		editxtidschedulle.setKeyListener(null);
		editxtx_id_med = (EditText)findViewById(R.id.edtxtIdMedic);
		editxtx_id_med.setKeyListener(null);

		
		//definimos los botones del selecciones de horas
		btn_hourFromAm = (Button)findViewById(R.id.btnHourFromAm);
		btn_hourToAm = (Button)findViewById(R.id.btnHourToAm);
		btn_hourFromPm = (Button)findViewById(R.id.btnHorariFromPm);
		btn_hourToPm = (Button)findViewById(R.id.btnHorariToPm);
		
		//definimos los campos donde se almacenaran las fechas seleccionadas
		editxthourFromAm = (EditText)findViewById(R.id.edtxtHourFromAm);
		editxthourFromAm.setKeyListener(null);
		editxthourToAm = (EditText)findViewById(R.id.edtxtHourToAm);
		editxthourToAm.setKeyListener(null);
		editxthourFromPm = (EditText)findViewById(R.id.edtxtHourFromPm);
		editxthourFromPm.setKeyListener(null);
		editxthourToPm = (EditText)findViewById(R.id.edtxtHourToPm);
		editxthourToPm.setKeyListener(null);
		
		//asignamos valores a las variables
		cal = Calendar.getInstance();
		day = cal.get(Calendar.DAY_OF_MONTH);
		month = cal.get(Calendar.MONTH);
		year = cal.get(Calendar.YEAR);   	
		pHour = cal.get(Calendar.HOUR_OF_DAY);
        pMinute = cal.get(Calendar.MINUTE);
        
        
        sp_duration = (Spinner)findViewById(R.id.sp_duration);
        String [] spin_arry = getResources().getStringArray(R.array.durations_quotes); 
        
        
        sp_duration.setAdapter(this.mAdapter = new CustomArrayAdapter<CharSequence>(this, spin_arry));
        
        
        
      //capturamos el evento para el boton de fecha desde
      		btnDateFrom.setOnClickListener(new OnClickListener() {
      				@Override
      				public void onClick(View v) {
      					// TODO Auto-generated method stub
      					
      					
      					showDialog(0);
      				}
      			});
      		//capturamos el evento para el boton de fecha hasta
      		btndateto.setOnClickListener(new OnClickListener() {
      				@Override
      				public void onClick(View v) {
      					// TODO Auto-generated method stub
      					
      					showDialog(1);
      				}
      			});
      		//capturamos el evento para el boton de hora am desde	   		
      		btn_hourFromAm.setOnClickListener(new OnClickListener() {	
      				@Override
      				public void onClick(View v) {
      					// TODO Auto-generated method stub
      					showDialog(2);
      				}
      			});
      		//capturamos el evento para el boton de hora am hasta	
      		btn_hourToAm.setOnClickListener(new OnClickListener() {	
      				@Override
      				public void onClick(View v) {
      					// TODO Auto-generated method stub
      					showDialog(3);
      				}
      			});
      		//capturamos el evento para el boton de hora pm desde		
      		btn_hourFromPm.setOnClickListener(new OnClickListener() {	
      				@Override
      				public void onClick(View v) {
      					// TODO Auto-generated method stub
      					showDialog(4);
      				}
      			});
      		//capturamos el evento para el boton de hora pm hasta		
      		btn_hourToPm.setOnClickListener(new OnClickListener() {	
      				@Override
      				public void onClick(View v) {
      					// TODO Auto-generated method stub
      					showDialog(5);
      				}
      			});
      		
      		
      		monday = (CheckBox)findViewById(R.id.chbxLun);
      		monday.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
				}
			});
      		
      	
    		//evento que mostrara un mensaje al usuario al momento de hacer la seleccion
      		sp_duration.setOnItemSelectedListener(new OnItemSelectedListener() {

    		@Override
    		public void onItemSelected(AdapterView<?> parentView,  View selectedItemView, int position, long id) {
    	   // sp_duration.setBackgroundResource(Color.GREEN);
    		Toast.makeText(parentView.getContext(), "Has Selecciondo " + parentView.getItemAtPosition(position).toString(), Toast.LENGTH_LONG).show();
    		
    		}

    		@Override
    		public void onNothingSelected(AdapterView<?> arg0) {}
    		});
	}
	
	//hora desde am
  	private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
  	            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  	                pHour = hourOfDay;
  	                pMinute = minute;
  	                editxthourFromAm.setText(new StringBuilder().append(pad(pHour)).append(":") .append(pad(pMinute)));  
  	            }
  	        };       
  	//hora hasta am   
  	private TimePickerDialog.OnTimeSetListener mTimeSetListener2 = new TimePickerDialog.OnTimeSetListener() {
  	            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  	                pHour = hourOfDay;
  	                pMinute = minute;
  	                editxthourToAm.setText(new StringBuilder().append(pad(pHour)).append(":") .append(pad(pMinute)));  
  	            }
  	        };
  	  //hora desde pm  
  	private TimePickerDialog.OnTimeSetListener mTimeSetListener3 = new TimePickerDialog.OnTimeSetListener() {
  	            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  	                pHour = hourOfDay;
  	                pMinute = minute;
  	                editxthourFromPm.setText(new StringBuilder().append(pad(pHour)).append(":") .append(pad(pMinute)));  
  	            }
  	        };
  	 //hora hasta pm 
  	private TimePickerDialog.OnTimeSetListener mTimeSetListener4 = new TimePickerDialog.OnTimeSetListener() {
  	            public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  	                pHour = hourOfDay;
  	                pMinute = minute;
  	                editxthourToPm.setText(new StringBuilder().append(pad(pHour)).append(":") .append(pad(pMinute)));  
  	            }
  	        };

  	private static String pad(int c) {
  	            if (c >= 10)
  	                return String.valueOf(c);
  	            else
  	                return "0" + String.valueOf(c);
  	        }
 	
  	@Override
  	protected Dialog onCreateDialog(int id) {
  		if(id == 0){
  			return new DatePickerDialog(this, datePickerListener, year, month, day);
  		}else if(id == 1) {
  			return new DatePickerDialog(this, datePickerListener2, year, month, day);
  		}
  		else if(id == 2) {
  			return new TimePickerDialog(this, mTimeSetListener, pHour, pMinute, false);
  		}
  		else if(id == 3) {
  			return new TimePickerDialog(this, mTimeSetListener2, pHour, pMinute, false);
  		}
  		else if(id == 4) {
  			return new TimePickerDialog(this, mTimeSetListener3, pHour, pMinute, false);
  		}
  		else if(id == 5) {
  			return new TimePickerDialog(this, mTimeSetListener4, pHour, pMinute, false);
  		}
  		return null;
  	}
	
  	// selector para fecha desde
  	private DatePickerDialog.OnDateSetListener datePickerListener = new DatePickerDialog.OnDateSetListener() {
  		public void onDateSet(DatePicker view, int selectedYear,
  				int selectedMonth, int selectedDay) {
  			editxt1.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
  					+ selectedYear);
  		}
  	};
  	// selector para fecha hasta
  	private DatePickerDialog.OnDateSetListener datePickerListener2 = new DatePickerDialog.OnDateSetListener() {
  		public void onDateSet(DatePicker view, int selectedYear,
  				int selectedMonth, int selectedDay) {
  			editxt2.setText(selectedDay + " / " + (selectedMonth + 1) + " / "
  					+ selectedYear);
  		}
  	};


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.openschedulle, menu);
		return true;
	}

}


class miCustomArrayAdapter<T> extends ArrayAdapter<T>
{
	

	
    public miCustomArrayAdapter(Context ctx, T [] objects)
    {
        super(ctx, android.R.layout.simple_spinner_item, objects);
    }

    //other constructors

    @Override
    public View getDropDownView(int position, View convertView, ViewGroup parent)
    {
        View view = super.getView(position, convertView, parent);

        //we know that simple_spinner_item has android.R.id.text1 TextView:         

        // if(isDroidX) {
            TextView text = (TextView)view.findViewById(android.R.id.text1);            
            text.setTextColor(Color.RED);//choose your color :)  
            text.setPadding(30, 20, 3, 2);
            //text.setPadding(left, top, right, bottom);
            view.setBackgroundResource(R.drawable.backup);
        //}

        return view;

    }
 
}


